package cn.iocoder.yudao.module.member.service.companyAccessCount;

import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.member.controller.admin.companyAccessCount.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.companyAccessCount.CompanyAccessCountDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.member.convert.companyAccessCount.CompanyAccessCountConvert;
import cn.iocoder.yudao.module.member.dal.mysql.companyAccessCount.CompanyAccessCountMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 访问记录 Service 实现类
 *
 * @author 周枫皓
 */
@Service
@Validated
public class CompanyAccessCountServiceImpl implements CompanyAccessCountService {

    @Resource
    private CompanyAccessCountMapper companyAccessCountMapper;

    @Override
    public Long createCompanyAccessCount(CompanyAccessCountCreateReqVO createReqVO) {
        // 插入
        CompanyAccessCountDO companyAccessCount = CompanyAccessCountConvert.INSTANCE.convert(createReqVO);
        companyAccessCountMapper.insert(companyAccessCount);
        // 返回
        return companyAccessCount.getId();
    }

    @Async
    @Override
    public Long createCompanyAccessCountIfPresent(@Valid CompanyAccessCountUpdateReqVO updateReqVO) {
        CompanyAccessCountDO companyAccessCountDO=companyAccessCountMapper.selectByParam(updateReqVO);
        if(companyAccessCountDO==null){
            updateReqVO.setAccessCount(1);
            BeanUtils.copyProperties(updateReqVO,companyAccessCountDO);
            companyAccessCountMapper.insert(companyAccessCountDO);
        }else{
            int count=companyAccessCountDO.getAccessCount()+1;
            companyAccessCountDO.setAccessCount(count);
            companyAccessCountMapper.updateById(companyAccessCountDO);
        }
        return companyAccessCountDO.getId();
    }

    @Override
    public void updateCompanyAccessCount(CompanyAccessCountUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateCompanyAccessCountExists(updateReqVO.getId());
        // 更新
        CompanyAccessCountDO updateObj = CompanyAccessCountConvert.INSTANCE.convert(updateReqVO);
        companyAccessCountMapper.updateById(updateObj);
    }

    @Override
    public void deleteCompanyAccessCount(Long id) {
        // 校验存在
        this.validateCompanyAccessCountExists(id);
        // 删除
        companyAccessCountMapper.deleteById(id);
    }

    private void validateCompanyAccessCountExists(Long id) {
        if (companyAccessCountMapper.selectById(id) == null) {
            throw exception(COMPANY_ACCESS_COUNT_NOT_EXISTS);
        }
    }

    @Override
    public CompanyAccessCountDO getCompanyAccessCount(Long id) {
        return companyAccessCountMapper.selectById(id);
    }

    @Override
    public List<CompanyAccessCountDO> getCompanyAccessCountList(Collection<Long> ids) {
        return companyAccessCountMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CompanyAccessCountDO> getCompanyAccessCountPage(CompanyAccessCountPageReqVO pageReqVO) {
        return companyAccessCountMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CompanyAccessCountDO> getCompanyAccessCountList(CompanyAccessCountExportReqVO exportReqVO) {
        return companyAccessCountMapper.selectList(exportReqVO);
    }

}
